package api

import (
	"net/http"

	"example.com/example/dao"
	"example.com/example/model"

	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/julienschmidt/httprouter"
)

var (
	_ = null.Bool{}
)

func configCustomProcessBaseGoodsRelRouter(router *httprouter.Router) {
	router.GET("/customprocessbasegoodsrel", GetAllCustomProcessBaseGoodsRel)
	router.POST("/customprocessbasegoodsrel", AddCustomProcessBaseGoodsRel)
	router.GET("/customprocessbasegoodsrel/:argBaseID/:argGoodsID", GetCustomProcessBaseGoodsRel)
	router.PUT("/customprocessbasegoodsrel/:argBaseID/:argGoodsID", UpdateCustomProcessBaseGoodsRel)
	router.DELETE("/customprocessbasegoodsrel/:argBaseID/:argGoodsID", DeleteCustomProcessBaseGoodsRel)
}

func configGinCustomProcessBaseGoodsRelRouter(router gin.IRoutes) {
	router.GET("/customprocessbasegoodsrel", ConverHttprouterToGin(GetAllCustomProcessBaseGoodsRel))
	router.POST("/customprocessbasegoodsrel", ConverHttprouterToGin(AddCustomProcessBaseGoodsRel))
	router.GET("/customprocessbasegoodsrel/:argBaseID/:argGoodsID", ConverHttprouterToGin(GetCustomProcessBaseGoodsRel))
	router.PUT("/customprocessbasegoodsrel/:argBaseID/:argGoodsID", ConverHttprouterToGin(UpdateCustomProcessBaseGoodsRel))
	router.DELETE("/customprocessbasegoodsrel/:argBaseID/:argGoodsID", ConverHttprouterToGin(DeleteCustomProcessBaseGoodsRel))
}

// GetAllCustomProcessBaseGoodsRel is a function to get a slice of record(s) from custom_process_base_goods_rel table in the custom_process database
// @Summary Get list of CustomProcessBaseGoodsRel
// @Tags CustomProcessBaseGoodsRel
// @Description GetAllCustomProcessBaseGoodsRel is a handler to get a slice of record(s) from custom_process_base_goods_rel table in the custom_process database
// @Accept  json
// @Produce  json
// @Param   page     query    int     false        "page requested (defaults to 0)"
// @Param   pagesize query    int     false        "number of records in a page  (defaults to 20)"
// @Param   order    query    string  false        "db sort order column"
// @Success 200 {object} api.PagedResults{data=[]model.CustomProcessBaseGoodsRel}
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbasegoodsrel [get]
// http "http://localhost:8080/customprocessbasegoodsrel?page=0&pagesize=20" X-Api-User:user123
func GetAllCustomProcessBaseGoodsRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	page, err := readInt(r, "page", 0)
	if err != nil || page < 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	pagesize, err := readInt(r, "pagesize", 20)
	if err != nil || pagesize <= 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	order := r.FormValue("order")

	if err := ValidateRequest(ctx, r, "custom_process_base_goods_rel", model.RetrieveMany); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	records, totalRows, err := dao.GetAllCustomProcessBaseGoodsRel(ctx, page, pagesize, order)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	result := &PagedResults{Page: page, PageSize: pagesize, Data: records, TotalRecords: totalRows}
	writeJSON(ctx, w, result)
}

// GetCustomProcessBaseGoodsRel is a function to get a single record from the custom_process_base_goods_rel table in the custom_process database
// @Summary Get record from table CustomProcessBaseGoodsRel by  argBaseID  argGoodsID
// @Tags CustomProcessBaseGoodsRel
// @ID argBaseID
// @ID argGoodsID
// @Description GetCustomProcessBaseGoodsRel is a function to get a single record from the custom_process_base_goods_rel table in the custom_process database
// @Accept  json
// @Produce  json
// @Param  argBaseID path int64 true "base_id"
// @Param  argGoodsID path int64 true "goods_id"
// @Success 200 {object} model.CustomProcessBaseGoodsRel
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError "ErrNotFound, db record for id not found - returns NotFound HTTP 404 not found error"
// @Router /customprocessbasegoodsrel/{argBaseID}/{argGoodsID} [get]
// http "http://localhost:8080/customprocessbasegoodsrel/1/1" X-Api-User:user123
func GetCustomProcessBaseGoodsRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argBaseID, err := parseUint64(ps, "argBaseID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argGoodsID, err := parseUint64(ps, "argGoodsID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_goods_rel", model.RetrieveOne); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	record, err := dao.GetCustomProcessBaseGoodsRel(ctx, argBaseID, argGoodsID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, record)
}

// AddCustomProcessBaseGoodsRel add to add a single record to custom_process_base_goods_rel table in the custom_process database
// @Summary Add an record to custom_process_base_goods_rel table
// @Description add to add a single record to custom_process_base_goods_rel table in the custom_process database
// @Tags CustomProcessBaseGoodsRel
// @Accept  json
// @Produce  json
// @Param CustomProcessBaseGoodsRel body model.CustomProcessBaseGoodsRel true "Add CustomProcessBaseGoodsRel"
// @Success 200 {object} model.CustomProcessBaseGoodsRel
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbasegoodsrel [post]
// echo '{"goods_id": 67,"base_id": 94}' | http POST "http://localhost:8080/customprocessbasegoodsrel" X-Api-User:user123
func AddCustomProcessBaseGoodsRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	customprocessbasegoodsrel := &model.CustomProcessBaseGoodsRel{}

	if err := readJSON(r, customprocessbasegoodsrel); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := customprocessbasegoodsrel.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	customprocessbasegoodsrel.Prepare()

	if err := customprocessbasegoodsrel.Validate(model.Create); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_goods_rel", model.Create); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	var err error
	customprocessbasegoodsrel, _, err = dao.AddCustomProcessBaseGoodsRel(ctx, customprocessbasegoodsrel)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, customprocessbasegoodsrel)
}

// UpdateCustomProcessBaseGoodsRel Update a single record from custom_process_base_goods_rel table in the custom_process database
// @Summary Update an record in table custom_process_base_goods_rel
// @Description Update a single record from custom_process_base_goods_rel table in the custom_process database
// @Tags CustomProcessBaseGoodsRel
// @Accept  json
// @Produce  json
// @Param  argBaseID path int64 true "base_id"// @Param  argGoodsID path int64 true "goods_id"
// @Param  CustomProcessBaseGoodsRel body model.CustomProcessBaseGoodsRel true "Update CustomProcessBaseGoodsRel record"
// @Success 200 {object} model.CustomProcessBaseGoodsRel
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbasegoodsrel/{argBaseID}/{argGoodsID} [put]
// echo '{"goods_id": 67,"base_id": 94}' | http PUT "http://localhost:8080/customprocessbasegoodsrel/1/1"  X-Api-User:user123
func UpdateCustomProcessBaseGoodsRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argBaseID, err := parseUint64(ps, "argBaseID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argGoodsID, err := parseUint64(ps, "argGoodsID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	customprocessbasegoodsrel := &model.CustomProcessBaseGoodsRel{}
	if err := readJSON(r, customprocessbasegoodsrel); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := customprocessbasegoodsrel.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	customprocessbasegoodsrel.Prepare()

	if err := customprocessbasegoodsrel.Validate(model.Update); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_goods_rel", model.Update); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	customprocessbasegoodsrel, _, err = dao.UpdateCustomProcessBaseGoodsRel(ctx,
		argBaseID, argGoodsID,
		customprocessbasegoodsrel)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, customprocessbasegoodsrel)
}

// DeleteCustomProcessBaseGoodsRel Delete a single record from custom_process_base_goods_rel table in the custom_process database
// @Summary Delete a record from custom_process_base_goods_rel
// @Description Delete a single record from custom_process_base_goods_rel table in the custom_process database
// @Tags CustomProcessBaseGoodsRel
// @Accept  json
// @Produce  json
// @Param  argBaseID path int64 true "base_id"// @Param  argGoodsID path int64 true "goods_id"
// @Success 204 {object} model.CustomProcessBaseGoodsRel
// @Failure 400 {object} api.HTTPError
// @Failure 500 {object} api.HTTPError
// @Router /customprocessbasegoodsrel/{argBaseID}/{argGoodsID} [delete]
// http DELETE "http://localhost:8080/customprocessbasegoodsrel/1/1" X-Api-User:user123
func DeleteCustomProcessBaseGoodsRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argBaseID, err := parseUint64(ps, "argBaseID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argGoodsID, err := parseUint64(ps, "argGoodsID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_goods_rel", model.Delete); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	rowsAffected, err := dao.DeleteCustomProcessBaseGoodsRel(ctx, argBaseID, argGoodsID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeRowsAffected(w, rowsAffected)
}
